package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;

import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@Slf4j
@RestController
@Api(tags = "套餐相关接口")
public class SetMealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation("添加套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<Object> add(@RequestBody SetmealDTO setmealDTO) {
        log.info("添加套餐");
        setmealService.add(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        PageResult pageResult = setmealService.queryPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("查询id为：{}的套餐", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<Object> deleteByIds(@RequestParam List<Long> ids) {
        log.info("删除id为：{} 的套餐", ids);

        // 判断是否进行删除
        if(!setmealService.isStatus(ids)) {
            return Result.error("删除失败，套餐中有已启用的优惠卷");
        }

        // 删除操作
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<Object> put(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改id为：{} 的套餐", setmealDTO.getId());
        setmealService.put(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用优惠卷")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<Object> setStatus(@PathVariable Integer status, Long id) {
        log.info("{} id为 {} 的套餐", status == 1?"启用":"禁用", id);

        // 检查是否能启用
        if(status == 1 && !setmealService.isAllDishesStatus(id)) {
            return Result.error("套餐关联的部分菜品未启售，启用失败");
        }

        // 设置status
        setmealService.setStatus(status, id);
        return Result.success();
    }
}
